﻿using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SuperSocket;
using SuperSocket.Command;
using SuperSocket.ProtoBase;
using SuperSocketV2Sample.Service.Commands;
using SuperSocketV2Sample.Service.Server;

namespace SuperSocketV2Sample.Service
{
    class Program
    {
        static async Task Main()
        {
            //创建宿主：用Package的类型和PipelineFilter的类型创建SuperSocket宿主。
            var host = SuperSocketHostBuilder.Create<StringPackageInfo, CommandLinePipelineFilter>()
                //注入TelnetService
                .UseHostedService<TelnetService<StringPackageInfo>>()
                //注入TelnetSession
                .UseSession<TelnetSession>()
                //注册用于处理连接、关闭的Session处理器
                .UseSessionHandler(async (session) =>
                {
                    Console.WriteLine($"{DateTime.Now} [SessionHandler] Session connected: {session.RemoteEndPoint}");
                    await Task.Delay(0);
                }, async (session, reason) =>
                {
                    Console.WriteLine($"{DateTime.Now} [SessionHandler] Session {session.RemoteEndPoint} closed: {reason}");
                    await Task.Delay(0);
                })
                //注入命令
                .UseCommand((commandOptions) =>
                {
                    commandOptions.AddCommand<AddCommand>();
                    commandOptions.AddCommand<DivCommand>();
                    commandOptions.AddCommand<MulCommand>();
                    commandOptions.AddCommand<SubCommand>();
                    commandOptions.AddCommand<EchoCommand>();
                })
                //配置日志
                .ConfigureLogging((hostCtx, loggingBuilder) =>
                {
                    loggingBuilder.AddConsole();
                })
                .Build();
            try
            {
                await host.RunAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
